package com.cn.demo.service.impl;

import com.cn.demo.entity.*;
import com.cn.demo.mapper.*;
import com.cn.demo.service.PageInfoUtilServer;
import com.cn.demo.service.TeacherService;
import com.cn.demo.utils.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private PageInfoUtilServer pageInfoUtilServer;


    @Autowired
    private CurriculumMapper curriculumMapper;

    @Autowired
    private OrganizationMapper organizationMapper;

    @Autowired
    private TeachingMapper teachingMapper;

    @Autowired
    private ExperimentMapper experimentMapper;

    @Autowired
    private InternshipMapper internshipMapper;

    @Autowired
    private InternshipLogMapper internshipLogMapper;

    @Autowired
    private DesignMapper designMapper;

    @Autowired
    private DesignTeamMapper designTeamMapper;

    @Autowired
    private EvaluateMapper evaluateMapper;



    @Override
    public Result add(Teacher teacher) {
        if(teacherMapper.getByTeacherId(teacher.getTeacherId())!=null){
            return Result.error("工号存在");
        }
        teacher.setPassword("123456");

        if (teacherMapper.add(teacher) > 0) {
            return Result.success();
        }
        return Result.error();
    }

    @Override
    public Result get(Integer page, Integer limit, Teacher teacher) {
        PageHelper.startPage(page, limit);
        List<Teacher> teacherList = teacherMapper.get(teacher);
        return Result.success(pageInfoUtilServer.currency(teacherList));
    }

    @Override
    public Result delete(String[] teacherIdList) {
        for (String teacherItem : teacherIdList) {
            //获得一个老师下面的所有课程
            Curriculum curriculum = new Curriculum();
            curriculum.setTeacherId(teacherItem);
            List<Curriculum> curriculumList = curriculumMapper.get(curriculum);
            for (Curriculum curriculumItem : curriculumList) {
                String curriculumId = curriculumItem.getCurriculumId();
                Organization organization = new Organization();
                organization.setCurriculumId(curriculumId);
                //获得一个课程下面的所有班级
                List<Organization> organizationList = organizationMapper.getOrganizationByCurriculumId(organization);
                for (Organization organizationItem : organizationList) {
                    //获得一个班级下面的所有学生
                    List<Student>  studentList = teachingMapper.getOrganizationStudentByOrganizationId(organizationItem.getOrganizationId(),null,null);
                    for (Student studentItem : studentList) {
                        //删除实验
                        experimentMapper.delete(curriculumItem.getCurriculumExperimentId(),studentItem.getStudentId());
                        evaluateMapper.delete(curriculumItem.getCurriculumExperimentId(),studentItem.getStudentId());
                        //删除实训
                        Internship internship = internshipMapper.getInternshipCorporation(studentItem.getStudentId(), curriculumItem.getCurriculumInternshipId());
                        if (internship!=null){
                            internshipLogMapper.delete(internship.getInternshipId());
                            internshipMapper.deleteByPrimaryKey(internship.getInternshipId());
                        }
                        evaluateMapper.delete(curriculumItem.getCurriculumInternshipId(),studentItem.getStudentId());
                        //删除课程设计
                        Design designSubject = designMapper.getDesignSubject(studentItem.getStudentId(), curriculumItem.getCurriculumDesignId());
                        if(designSubject!=null){
                            List<DesignTeam> designTeamList = designSubject.getDesignTeamList();
                            designTeamMapper.delete(designSubject.getDesignId(),studentItem.getStudentId());
                            evaluateMapper.delete(curriculumItem.getCurriculumDesignId(),studentItem.getStudentId());
                            if (designTeamList.size()==1){
                                //没有人直接删除设计
                                designMapper.deleteByPrimaryKey(designSubject.getDesignId());
                            }
                        }
                        List<String> StringListStudent = new ArrayList<>();
                        StringListStudent.add(studentItem.getStudentId());
                        //删除班级学生
                        teachingMapper.organizationStudentDelete(StringListStudent,organizationItem.getOrganizationId());
                    }
                    //删除班级
                    organizationMapper.deleteByPrimaryKey(organizationItem.getOrganizationId());
                }
                //删除课程
                curriculumMapper.deleteByPrimaryKey(curriculumItem.getCurriculumId());
            }
            //删除老师
            teacherMapper.deleteByPrimaryKey(teacherItem);
        }
        return Result.success();
    }

    @Override
    public Result update(Teacher teacher) {
        if (teacherMapper.getByTeacherId(teacher.getTeacherId()) == null) {
            return Result.error("教师ID不存在");
        }
        if (teacherMapper.upDateByTeacherId(teacher) > 0) {
            return Result.success();
        }
        return Result.error();
    }

    @Override
    public Result getByTeacherId(String teacherId) {
        return Result.success(teacherMapper.getByTeacherId(teacherId));
    }


    @Override
    public Result modifyPassword(String teacherId, String oldPassword, String affirmPassword) {
        if (teacherMapper.checkInfo(teacherId, oldPassword) != null) {
            if (teacherMapper.updatePassword(teacherId, affirmPassword)>0) {
                return Result.success();
            }
            return Result.error();
        }
        return Result.error("密码错误");
    }

    @Override
    public Result login(Teacher teacher) {
        Teacher login = teacherMapper.login(teacher);
        if (login == null) {
            return Result.error("用户名或密码错误");
        } else {
            String token = UUID.randomUUID() + "";
            login.setToken(token);
            if (teacherMapper.updateToken(token, login.getTeacherId()) == 1) {
                return  Result.success(login);
            } else {
                return  Result.error();
            }
        }
    }
}